******************************************************************************
*                                                                            *
*   Replication file for "Hierarchical Oversight and the Value of Public     *
*         Office: Evidence from Colonial Peru" By Jenny Guardado             *
*                                                                            *
*                              June  2021                                    *   
*                                                                            *
******************************************************************************   


*Set Up

clear all
set more off
*cd "C:\."

*installing necessary packages
cap ssc install outreg2

log using LogMainTablesFigures, replace


*******************
***FIGURE 3
*******************

use fig2, clear

label var per_spaincomp "% Spanish (All Members)"
label var shsold "% New Entrants by Purchase"

twoway (line per_spaincomp year, lcolor(black) sort) (line shsold year, sort yaxis(2) lcolor(black) lpattern(dash)), legend( on rows(1) size(small) region(col(white))) xtitle("Year") ytitle("% Spanish (All Members)") graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))

graph save fig2, replace


*******************
***TABLE 1
*******************


use indivmemberslima.dta, clear

tab promotion purchase if promotion==1
tab death purchase if death==1
tab removed purchase if removed==1
tab retire purchase if retire==1


*\begin{tabular}{lccccc}
*                          & Promotion & Death & Removed & Retired & Total \\ \hline
*Both                      & 1   & 21   &  8     & 3     &  33 \\  \hline
*Non-purchaser ("Careerist")& 0  & 9    & 0       & 2    &  11  \\ 
*Purchaser ("Non Careerist") & 1  & 12    & 8      & 1    &  22  \\ 
*\end{tabular}
*\end{table}


*******************
***TABLE 2: OLS
*******************

*new members data

use newmembersdata.dta, clear

#delimit;
local replace replace;
foreach var of varlist difprice lgprice {;
xi: xtreg `var' chshsold distfuture  obyear1-obyear8 i.year, fe cluster(provcode);
sum `var' if e(sample);
local mean = r(mean);
outreg2 using  Table2.tex, `replace' dec(2) nocons noni
adds("Provinces", e(N_clust), "Mean DV", `mean');
local replace;
};
#delimit cr


#delimit;
local replace append;
foreach var of varlist difprice lgprice {;
xi: xtreg `var' chshspain distfuture  obyear1-obyear8 i.year, fe cluster(provcode);
sum `var' if e(sample);
local mean = r(mean);
outreg2 using  Table2.tex, `replace' dec(2) nocons noni
adds("Provinces", e(N_clust), "Mean DV", `mean');
local replace;
};
#delimit cr

*composition data

use composition.dta, clear

#delimit;
local replace append;
foreach var of varlist difprice lgprice {;
xi: xtreg `var' chshspain distfuture  obyear1-obyear8 i.year, fe cluster(provcode);
sum `var' if e(sample);
local mean = r(mean);
outreg2 using  Table2.tex, `replace' dec(2) nocons noni
adds("Provinces", e(N_clust), "Mean DV", `mean');
local replace;
};
#delimit cr


*******************
***TABLE 3: IV 
*******************

*new members data

use newmembersdata.dta, clear


#delimit;
local replace replace;
foreach var of varlist difprice lgprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9, fe cluster(provcode) first
 partial (obyear* distfuture _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table3.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace;
};
#delimit cr



#delimit;
local replace append;
foreach var of varlist difprice lgprice {;
xi: xtivreg2 `var' (chshspain = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9, fe cluster(provcode) first
 partial (obyear* distfuture _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table3.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace;
};
#delimit cr


*composition data

use composition.dta, clear


#delimit;
local replace append;
foreach var of varlist difprice lgprice {;
xi: xtivreg2 `var' (chshspain = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9, fe cluster(provcode) first
 partial (obyear* distfuture _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table3.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace;
};
#delimit cr


*******************
***TABLE 4: Other Outcomes (Uprisings) 
*******************

*new members data

use newmembersdata, clear


#delimit;
local replace replace;
foreach var of varlist chanyreb chgovreb {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9, fe  cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table4.tex, `replace' dec(3) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


#delimit;
local replace append;
foreach var of varlist chanyreb chgovreb {;
xi: xtivreg2 `var' (chshspain = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9, fe  cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table4.tex, `replace' dec(3) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr



*composition data

use composition.dta, clear


#delimit;
local replace append;
foreach var of varlist chanyreb chgovreb {;
xi: xtivreg2 `var' (chshspain = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9, fe  cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table4.tex, `replace' dec(3) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr



*******************
***TABLE 5: Heterogenous Effects
*******************

***************
*Panels A and B
***************

*new members data

use newmembersdata, clear

*Repartimiento 

#delimit;
local replace replace;
foreach var of varlist difprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & rep50==1, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5A.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


#delimit;
local replace replace;
foreach var of varlist lgprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & rep50==1, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5B.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


#delimit;
local replace append;
foreach var of varlist difprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8  i.year distfuture if distfuture<=9 & rep50==0, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5A.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr

#delimit;
local replace append;
foreach var of varlist lgprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8  i.year distfuture if distfuture<=9 & rep50==0, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5B.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr



*Tributarios

#delimit;
local replace append;
foreach var of varlist difprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & tribhigh==1, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5A.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr

#delimit;
local replace append;
foreach var of varlist lgprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & tribhigh==1, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5B.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


#delimit;
local replace append;
foreach var of varlist difprice  {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & tribhigh==0, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5A.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr

#delimit;
local replace append;
foreach var of varlist lgprice  {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & tribhigh==0, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5B.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


*Agricultural Suitability

#delimit;
local replace append;
foreach var of varlist difprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & agrhigh==1, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5A.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


#delimit;
local replace append;
foreach var of varlist lgprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & agrhigh==1, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5B.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


#delimit;
local replace append;
foreach var of varlist difprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & agrhigh==0, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5A.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


#delimit;
local replace append;
foreach var of varlist lgprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & agrhigh==0, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5B.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr



***************
*Panels C and D
***************

*Elevation

#delimit;
local replace replace;
foreach var of varlist difprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & high==1, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5C.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr



#delimit;
local replace replace;
foreach var of varlist lgprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & high==1, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5D.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


#delimit;
local replace append;
foreach var of varlist difprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8  i.year distfuture if distfuture<=9 & high==0, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5C.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


#delimit;
local replace append;
foreach var of varlist lgprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8  i.year distfuture if distfuture<=9 & high==0, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5D.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr



*Mining


#delimit;
local replace append;
foreach var of varlist difprice  {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & mine==1, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5C.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


#delimit;
local replace append;
foreach var of varlist lgprice  {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & mine==1, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5D.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


#delimit;
local replace append;
foreach var of varlist difprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & mine==0, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5C.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


#delimit;
local replace append;
foreach var of varlist lgprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & mine==0, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5D.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr



*Commercial trails

#delimit;
local replace append;
foreach var of varlist difprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & circuito==1, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5C.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


#delimit;
local replace append;
foreach var of varlist lgprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & circuito==1, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5D.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


#delimit;
local replace append;
foreach var of varlist difprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & circuito==0, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using Table5C.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


#delimit;
local replace append;
foreach var of varlist lgprice {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9 & circuito==0, fe cluster(provcode)
 partial (distfuture obyear* _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table5D.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr



*******************
***TABLE 6: Audiencia Members' Types
*******************

*new members data

use newmembersdata, clear

********
*PANEL A
********

#delimit;
local replace replace;
foreach var of varlist chshspuniv chshamuniv chshba chshlic chshdr {;
xi: ivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9, cluster(provcode)
first partial (obyear* distfuture _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table6A.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


********
*PANEL B
********

#delimit;
local replace replace;
foreach var of varlist chshfamcon chshdisp chshlimawife chshprop chshmerchant {;
xi: xtivreg2 `var' (chshsold = totspanishdeath) obyear1-obyear8 i.year distfuture if distfuture<=9, fe cluster(provcode)
first partial (obyear* distfuture _I*);
sum `var' if e(sample);
local mean = r(mean);
local fstat = e(widstat);
outreg2 using  Table6B.tex, `replace' dec(2) nocons noni nor
adds("Provinces", e(N_clust), "Mean DV", `mean', "F-Stat", `fstat');
local replace append;
};
#delimit cr


log close


